var $LISTNGTITLE=[],$LISTNGDATE=[],$LISTNGSOURCE=[],$LISTNGLINK=[],$CURNGID;
NEWSGROUP=function(){}
NEWSGROUP.prototype.list=function(groupid){
	var fncSuc = XHR.Delegate(this,this.listSuc);
	var param = {action:"listNewsInGroup",groupId:groupid};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.groupid = groupid;
}

NEWSGROUP.prototype.listSuc=function(r){
	var xml = r.xml;
	if(checklogin(xml)==false){
		window.location = $PN;
		return;
	}
	var html = "";
	if(getRV(xml)){
		$LISTNGID=[],$LISTNGTITLE=[],$LISTNGDATE=[],$LISTNGSOURCE=[],$LISTNGLINK=[];
		var news = xml.getElementsByTagName("news");
		for(var i=0; i < news.length; i++){
			var id = news[i].getAttribute("newsid");
			$LISTNGTITLE[id]=news[i].getAttribute("title");
			$LISTNGDATE[id]=news[i].getAttribute("date");
			$LISTNGSOURCE[id]=news[i].getAttribute("source");
			$LISTNGLINK[id]=news[i].getAttribute("link");			
			html+="<div style=\"padding:3px\">" +
					"<span style=\"cursor:pointer\" onClick=\"removeNewsGroup('"+id+"','"+this.groupid+"')\">[Xóa]</span>" +
					"&nbsp;&nbsp;<span style=\"cursor:pointer\" onClick=\"clickEdit('"+id+"')\">[Chỉnh sửa]</span>&nbsp;&nbsp;"+
					"<a style=\"color:blue\" target=\"_blank\" href=\""+news[i].getAttribute("link")+"\">"+news[i].getAttribute("title")+" - ("+news[i].getAttribute("source")+")" +"</a>"+
					"</div>";
		}
		$("totalnews").innerHTML = news.length;
		$("listLink").innerHTML = html;
		$("listLinkDiv").style.display="block";
	}else{
		alert("Fail");
	}
}

NEWSGROUP.prototype.remove=function(newsid, groupid){
	var fncSuc = XHR.Delegate(this,this.removeSuc);
	var param = {action:"deleteNewsInGroup",groupId:groupid,newsId:newsid};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.groupId = groupid;
}
NEWSGROUP.prototype.removeSuc=function(r){
	var xml = r.xml;
	if(checklogin(xml)==false){
		window.location = $PN;
		return;
	}
	var html = "";
	if(getRV(xml)){
		viewNewsGroup(this.groupId);
	}else{
		alert("Fail");
	}
}
NEWSGROUP.prototype.add=function(groupId,title,link,date,source){
	var fncSuc = XHR.Delegate(this,this.removeSuc);
	var param = {action:"addNewsToGroup",groupId:groupId,title:title,link:link,date:date,source:source};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.groupId = groupId;
}

NEWSGROUP.prototype.update=function(groupId,newsId,title,link,date,source){
	var fncSuc = XHR.Delegate(this,this.removeSuc);
	var param = {action:"updateNewsInGroup",newsId:newsId,groupId:groupId,title:title,link:link,date:date,source:source};
	new XHR("post",fncSuc).send($PN+"/DoAdmin",param);
	this.groupId = groupId;
}

function viewNewsGroup(groupId){
	new NEWSGROUP().list(groupId);
}
function removeNewsGroup(newsId,groupId){
	if(confirm("Bạn có chắc muốn xóa?")){
		new NEWSGROUP().remove(newsId,groupId)
	}
}
function addNewsToGroup(){
	var title = $N("group_title")[0].value;
	var link = $N("group_link")[0].value;
	var source = $N("group_source")[0].value;
	var date = $N("group_date")[0].value;		
	if(title!=""&&link!=""&&source!=""&&date!="")
	new NEWSGROUP().add($NEWSID,title,link,date,source);
}
function updateNewsInGroup(){
	var title = $N("group_title")[0].value;
	var link = $N("group_link")[0].value;
	var source = $N("group_source")[0].value;
	var date = $N("group_date")[0].value;		
	if(title!=""&&link!=""&&source!=""&&date!="")
	new NEWSGROUP().update($NEWSID,$CURNGID,title,link,date,source);
}
function clickEdit(id){
	$CURNGID=id;
	$N("group_title")[0].value=$LISTNGTITLE[id];
	$N("group_link")[0].value=$LISTNGLINK[id];;
	$N("group_source")[0].value=$LISTNGSOURCE[id];;
	$N("group_date")[0].value=$LISTNGDATE[id];;		
}